package com.dms.web.controller.system;

import java.util.List;

import com.dms.system.domain.dto.SysTodayMissionDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.dms.common.annotation.Log;
import com.dms.common.enums.BusinessType;
import com.dms.system.domain.SysTodayMission;
import com.dms.system.service.ISysTodayMissionService;
import com.dms.common.core.controller.BaseController;
import com.dms.common.core.domain.AjaxResult;
import com.dms.common.utils.poi.ExcelUtil;
import com.dms.common.core.page.TableDataInfo;

/**
 * 每日任务Controller
 * 
 * @author xlong_zhu
 * @date 2021-04-05
 */
@Controller
@RequestMapping("/system/todayMission")
public class SysTodayMissionController extends BaseController
{
    private String prefix = "system/todayMission";

    @Autowired
    private ISysTodayMissionService sysTodayMissionService;

    @GetMapping()
    public String mission()
    {
        return prefix + "/todayMission";
    }

    /**
     * 查询每日任务列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTodayMissionDTO sysTodayMission, Long userId ,@RequestParam(defaultValue = "true") boolean isPagination)
    {
        List<SysTodayMissionDTO> list;
        if(isPagination){
            // startPage必须紧跟下面全查询的代码，分页才会生效
            startPage();
            list = sysTodayMissionService.selectSysTodayMissionList(sysTodayMission);
        }else{
            list = sysTodayMissionService.selectSysTodayMissionList(sysTodayMission,userId);
        }
        return getDataTable(list);
    }

    /**
     * 导出每日任务列表
     */
    @RequiresPermissions("system:todayMission:export")
    @Log(title = "每日任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTodayMissionDTO sysTodayMission)
    {
        List<SysTodayMissionDTO> list = sysTodayMissionService.selectSysTodayMissionList(sysTodayMission);
        ExcelUtil<SysTodayMissionDTO> util = new ExcelUtil<SysTodayMissionDTO>(SysTodayMissionDTO.class);
        return util.exportExcel(list, "todayMission");
    }

    /**
     * 新增每日任务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存每日任务
     */
    @Log(title = "每日任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTodayMission sysTodayMission)
    {
        return toAjax(sysTodayMissionService.insertSysTodayMission(sysTodayMission));
    }

    /**
     * 修改每日任务
     */
    @GetMapping("/edit/{todayMissionId}")
    public String edit(@PathVariable("todayMissionId") Long todayMissionId, ModelMap mmap)
    {
        SysTodayMission sysTodayMission = sysTodayMissionService.selectSysTodayMissionById(todayMissionId);
        mmap.put("sysTodayMission", sysTodayMission);
        return prefix + "/edit";
    }

    /**
     * 修改保存每日任务
     */
    @Log(title = "每日任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTodayMission sysTodayMission)
    {
        return toAjax(sysTodayMissionService.updateSysTodayMission(sysTodayMission));
    }

    /**
     * 删除每日任务
     */
    @Log(title = "每日任务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTodayMissionService.deleteSysTodayMissionByIds(ids));
    }
}
